package com.msconfig.mvp.view.fragment;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.bean.deviceInfoBean.TagDeviceInfo;
import com.msconfig.device.CommandParseManage;
import com.msconfig.event.BluetoothInfoEvent;
import com.msconfig.event.DeviceUpdateEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.utils.SpUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class EquipmentInformationFragment extends BaseMvpFragment implements View.OnClickListener {
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private TextView tvFirmwareVersion;
    private TextView tvMcuFirmwareVersion;
    private TextView tvBluetoothAdress;
    private TextView tvBatteryLevel;
    private TextView tvBatteryVoltage;
    private TextView tvBluetoothSignal;
    private TextView tvFreeStorage;
    private TextView tvTotalStorageSpace;

    private Context context;
    private BluetoothDevice device;
    private TagDeviceInfo deviceInfo;

    public static EquipmentInformationFragment newInstance() {
        Bundle args = new Bundle();
        EquipmentInformationFragment fragment = new EquipmentInformationFragment();
        fragment.setArguments(args);
        return fragment;
    }

    //连接成功后返回的蓝牙信号强度和地址
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showBlueToothInfo(BluetoothInfoEvent event) {
        device=event.getDevice();
        if(device==null)
            return;
        tvBluetoothAdress.setText(device.getAddress());
        //根据地址拿到信号强度
        if(SpUtils.obtain().getString(device.getAddress())==null){
            tvBluetoothSignal.setText(getString(R.string.bluetooth_signal,"0"));
        }else{
            tvBluetoothSignal.setText(getString(R.string.bluetooth_signal,SpUtils.obtain().getString(device.getAddress())));
        }
    }

    //GETALL成功
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showAnalysisHostDataSuccess(DeviceUpdateEvent.UpdateDialogGetRtkSuccessStatus event) {
        tvFirmwareVersion.setText(deviceInfo.basicInfo.mFirmwareVersion);
        tvMcuFirmwareVersion.setText(deviceInfo.basicInfo.mMcuFirmwareVersion);
        tvBatteryLevel.setText(deviceInfo.basicInfo.mBatteryLevel);
        tvBatteryVoltage.setText(deviceInfo.basicInfo.mBatteryVoltage);
        tvFreeStorage.setText(getString(R.string.equipment_info_storage, Integer.parseInt(deviceInfo.basicInfo.mFreeStorage)/1024/1024));
        tvTotalStorageSpace.setText(getString(R.string.equipment_info_storage, Integer.parseInt(deviceInfo.basicInfo.mTotalStorageSpace)/1024/1024));
    }

    //电池电量信息更新
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showGetPowerLevelSuccess(DeviceUpdateEvent.UpdateDialogGetPowerLevelSuccessStatus event) {
        tvBatteryLevel.setText(deviceInfo.basicInfo.mBatteryLevel);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_equipment_formation;
    }

    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        tvFirmwareVersion = (TextView)view.findViewById( R.id.tv_firmware_version );
        tvMcuFirmwareVersion = (TextView)view.findViewById( R.id.tv_mcu_firmware_version);
        tvBatteryLevel = (TextView)view.findViewById( R.id.tv_battery_level );
        tvBluetoothAdress = (TextView)view.findViewById( R.id.tv_bluetooth_adress );
        tvBluetoothSignal = (TextView)view.findViewById( R.id.tv_bluetooth_signal);
        tvBatteryVoltage = (TextView)view.findViewById( R.id.tv_battery_voltage );
        tvFreeStorage = (TextView)view.findViewById( R.id.tv_free_storage );
        tvTotalStorageSpace = (TextView)view.findViewById( R.id.tv_total_storage_space );

    }

    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);

    }

    //当Activity与fragment建立关联时调用
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_equipment_info);
        deviceInfo=CommandParseManage.getInstance().getDeviceInfo();
    }


    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            backTo();
        }

    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

}
